package com.imti.mall.order.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.imti.mall.model.order.dto.ReturnApplyParam;
import com.imti.mall.model.order.pojo.OmsApplyAudit;
import com.imti.mall.model.order.pojo.OmsOrderItem;
import com.imti.mall.model.order.pojo.OmsOrderReturnApply;
import com.imti.mall.model.order.query.ReturnApplyQuery;
import com.imti.mall.order.mapper.OmsOrderReturnApplyMapper;
import com.imti.mall.order.mapper.OrderItemMapper;
import com.imti.mall.order.service.OmsOrderReturnApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper,OmsOrderReturnApply> implements OmsOrderReturnApplyService{

  @Autowired
  private OmsOrderReturnApplyMapper omsOrderReturnApplyMapper;

  @Autowired
  private OrderItemMapper orderItemMapper;


  @Override
  public List<OmsOrderReturnApply> list(ReturnApplyQuery returnApplyQuery, Integer pageSize, Integer pageNum) {
    PageHelper.startPage(pageNum, pageSize);
    return omsOrderReturnApplyMapper.getList(returnApplyQuery);
  }

  @Override
  public int updateStatusById(Long id,OmsApplyAudit omsApplyAudit) {
    return omsOrderReturnApplyMapper.updateStatusById(id,omsApplyAudit);
  }

  @Override
  public Long countReturnApply(ReturnApplyQuery returnApplyQuery) {
    if("admin".equals(returnApplyQuery.getReturnName())){
      returnApplyQuery.setReturnName("");
    }
    return omsOrderReturnApplyMapper.countReturnApply(returnApplyQuery);
  }

  @Override
  //@Transactional
  public int addOmsOrderReturnApply(ReturnApplyParam returnApplyParam) {
    //通过订单id获取商品信息list
    List<OmsOrderItem> omsOrderItemList = orderItemMapper.findItemByOrderId(returnApplyParam.getOrderId());
    int result = 0;
    for (int i=0;i<omsOrderItemList.size();i++){
      //商品id
      Long productId = omsOrderItemList.get(i).getProductId();
      int status=0;
      //商品图片
      String productPic = omsOrderItemList.get(i).getProductPic();
      //商品名称
      String productName = omsOrderItemList.get(i).getProductName();
      //商品品牌
      String productBrand = omsOrderItemList.get(i).getProductBrand();
      //商品销售属性
      String productAttr = omsOrderItemList.get(i).getProductAttr();
      //退货数量
      Integer productCount = omsOrderItemList.size();
      //商品单价
      BigDecimal productPrice = omsOrderItemList.get(i).getProductPrice();
      //还需订单id，订单编号，申请时间，会员用户名，退款金额，退货人姓名，退货人电话，商品实付金额
      if(returnApplyParam.getReturnApplyAmountList()!=null){
        //有凭证图片
        for(int j=0;j<returnApplyParam.getProofPicList().size();j++){
          OmsOrderReturnApply omsOrderReturnApply = new OmsOrderReturnApply(returnApplyParam.getOrderId(),
                  productId,returnApplyParam.getOrderSn(),returnApplyParam.getCreateTime(),returnApplyParam.getReturnName(),
                  returnApplyParam.getReturnApplyAmountList().get(i),returnApplyParam.getReturnName(),
                  returnApplyParam.getReturnPhone(),status,productPic,productName,productBrand,
                  productAttr,productCount,productPrice,returnApplyParam.getProductRealPriceList().get(i),
                  returnApplyParam.getReson(),returnApplyParam.getDescription(),
                  returnApplyParam.getProofPicList().get(j));
          result = omsOrderReturnApplyMapper.insert(omsOrderReturnApply);
        }
      }else{
        //没有凭证图片
        //Long productId, String orderSn, String createTime, String memberUsername, BigDecimal returnAmount, String returnName, String returnPhone, Integer status, Date handleTime, String productPic, String productName, String productBrand, String productAttr, Integer productCount, BigDecimal productPrice, BigDecimal productRealPrice, String reason, String description, String proofPics, String handleNote, String handleMan, String receiveMan, Date receiveTime, String receiveNote
        OmsOrderReturnApply omsOrderReturnApply = new OmsOrderReturnApply(returnApplyParam.getOrderId(),
                productId,returnApplyParam.getOrderSn(),returnApplyParam.getCreateTime(),returnApplyParam.getReturnName(),
                returnApplyParam.getReturnApplyAmountList().get(i),returnApplyParam.getReturnName(),
                returnApplyParam.getReturnPhone(),status,productPic,productName,productBrand,
                productAttr,productCount,productPrice,returnApplyParam.getProductRealPriceList().get(i),
                returnApplyParam.getReson(),returnApplyParam.getDescription());
        result = omsOrderReturnApplyMapper.insert(omsOrderReturnApply);
      }
    }
    return result;
  }


}
